package com.lijian.rest.service;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.lijian.mapper.TbItemCatMapper;
import com.lijian.pojo.TbItemCat;
import com.lijian.pojo.TbItemCatExample;
import com.lijian.pojo.TbItemCatExample.Criteria;
import com.lijian.rest.pojo.CatNode;
import com.lijian.rest.pojo.ItemCatResult;

/**
 * <p>Title: ItemCatServiceImpl.java</p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2017</p>
 * @author lijian
 * @date 2017年4月15日上午8:33:46
 * @version 1.0
 */
@Service
public class ItemCatServiceImpl implements ItemCatService {

	@Autowired
	private TbItemCatMapper itemCatMapper;
	/**
	 * <p>Title: getItemCatList</p>
	 * <p>Description: </p>
	 * @return
	 * @see com.lijian.rest.service.ItemCatService#getItemCatList()
	 */
	@Override
	public ItemCatResult getItemCatList() {
		List itemCatLIst = getItemCatLIst(0l);
		ItemCatResult res=new ItemCatResult();
		res.setData(itemCatLIst);
		return res;
	}

	private List getItemCatLIst(Long parentId){
		TbItemCatExample example=new TbItemCatExample();
		Criteria createCriteria = example.createCriteria();
		createCriteria.andParentIdEqualTo(parentId);
		java.util.List<TbItemCat> list = itemCatMapper.selectByExample(example);
		List res=new ArrayList();
		int i=0;
		for(TbItemCat item:list){
			
			if(i>=14){
				break;
			}
			
			if(item.getIsParent()){
				
				CatNode node=new CatNode();
				node.setUrl("/products/"+item.getId()+".html");
				if(item.getParentId()==0){
					i++;
					node.setName("<a href='/products/"+item.getId()+".html'>"+item.getName()+"</a>");
				}else{
					node.setName(item.getName());
				}
				node.setItems(getItemCatLIst(item.getId()));
				res.add(node);
			}else{//如果是叶子节点
				res.add("/products/"+item.getId()+".html|"+item.getName());
			}
		}
		return res;
	}
}
